<template>
	<view class="comment">
		<Header v-model="keywords" @change="handleChange"></Header>
		<view class="list" v-if="list.length">
			<custom-waterfalls-flow :value="list" imageKey="firstMedia" @wapperClick="toCommentDetail" @imageClick="toCommentDetail">
				<!-- #ifdef MP-WEIXIN -->
				<view class="item" v-for="(item, index) in list" :key="index" slot="slot{{index}}">
					<view class="title">{{item.content}}</view>
					<view class="user">
						<u--image :src="item.avatar ? item.avatar : '/static/images/avatar-icon.png'" width="20" height="20" shape="circle" ></u--image>
						<text>{{item.userName}}</text>
					</view>
				</view>
				<!-- #endif -->
				<!-- #ifndef MP-WEIXIN -->
				<template v-slot:default="item">
					<view class="item">
						<view class="title">{{item.content}}</view>
						<view class="user">
							<image :src="item.creatorAvatar? item.creatorAvatar : '/static/images/avatar-icon.png'" mode=""></image>
							<text>{{item.userName}}</text>
						</view>
					</view>
				</template>
				<!-- #endif -->
			</custom-waterfalls-flow>
			<u-loadmore :status="loadmoreStatus" :marginTop="30" :marginBottom="30" line />
		</view>
		<view class="empty" v-else>
			<u-empty mode="search" :iconSize="120" text="研学纪实为空"></u-empty>
		</view>
		<LoginModal ref="loginModal" />
	</view>
</template>

<script>
	import Header from '@/components/header.vue';
	import {
		checkLogin
	} from '@/utils/index.js';

	export default {
		components: {
			Header,
		},
		data() {
			return {
				keywords: '',
				page: 1,
				size: 10,
				list: [],
				loadmoreStatus: 'loadmore',
			}
		},
		onLoad() {
			this.getList();
		},
		onPullDownRefresh() {
			this.page = 1;
			this.list = [];
			this.getList();
			uni.stopPullDownRefresh();
		},
		onReachBottom() {
			if (this.loadmoreStatus === 'nomore') return;
			this.loadmoreStatus = 'loading';
			this.page += 1;
			this.getList();
		},
		methods: {
			async getList() {
				const res = await this.$api('/study/mobile/comment/page', 'get', {
					page: this.page,
					limit: this.size,
					keywords: this.keywords,
				});

				this.loadmoreStatus = 'loadmore';
				this.list = this.list.concat(res.data.list);

				if (res.data.list.length < this.size) {
					this.loadmoreStatus = 'nomore';
					return;
				}
			},

			handleChange(e) {
				this.keywords = e;
				this.page = 1;
				this.list = [];
				this.loadmoreStatus = 'loadmore';
				this.getList();
			},

			toCommentDetail(item) {
				if(checkLogin()) {
					uni.navigateTo({
						url: `/pages/study/comment-detail?id=${item.id}`
					})
				} else {
					this.$refs.loginModal.open();
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.comment {
		padding: 0 30rpx 60rpx;
		
		.list {
			margin-top: 30rpx;
		}

		.title {
			padding: 20rpx;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
		}

		.user {
			display: flex;
			align-items: center;
			color: #444;
			padding: 0rpx 10rpx 30rpx;
			
			text {
				padding-left: 10rpx;
			}
		}
	}
</style>